// 配置路由的地方
import Vue from "vue";
import VueRouter from 'vue-router'
import routes from "@/router/routes";
//引入路由组件

// 使用插件
Vue.use(VueRouter)

//先把VueRouter原型对象的push, 先保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

//重写push和replace方法
//第一个参数: 告诉原来的push方法, 你往哪里跳转(传递哪些参数)
//第二个参数: 成功回调
//第三个参数: 失败回调
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        // call和apply区别
        // 相同点: 都可以调用函数一次, 都可以篡改函数的上下文一次
        // 不同点: call和apply传递参数: call传递参数用逗号隔开, apply用数组
        originPush.call(this, location, resolve, reject);
    } else {
        originPush.call(this, location, () => {
        }, () => {
        })
    }
};
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        originPush.call(this, location, resolve, reject);
    } else {
        originPush.call(this, location, () => {
        }, () => {
        })
    }
};


// 配置路由
export default new VueRouter({
    //配置路由
    routes,
    scrollBehavior(to, from, savePosition) {
        return {y: 0}
    },
})